import importlib
import os
import sys
import time
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
from watchdog_test import ReloadHandler
# from workflow import *

def main():
    workflow_dir = os.path.abspath('workflow')
    project_dir = os.path.dirname(workflow_dir)
    if project_dir not in sys.path:
        sys.path.insert(0, project_dir)

    # 确保workflow是一个包
    init_py = os.path.join(workflow_dir, '__init__.py')
    if not os.path.exists(init_py):
        open(init_py, 'a').close()

    event_handler = ReloadHandler(workflow_dir)
    observer = Observer()
    observer.schedule(event_handler, workflow_dir, recursive=True)
    observer.start()

    try:
        while True:
            user_input = input()
            print(user_input)
            module_name, func_name = user_input.split('.')
            module_key = f"workflow.{module_name}"
            module = sys.modules.get(module_key) or importlib.import_module(module_key)
            func = getattr(module, func_name)
            func()
            # a.func1()
            # time.sleep(2)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

if __name__ == '__main__':
    main()